import { createI18n } from 'vue-i18n'
// import useAppStore from '@/store/modules/app'
import { listLangByLocale } from '@/api/system/commonlang.js'
import defaultSettings from '@/settings'
import cache from '@/plugins/cache'
export const language = computed(() => {
  // return useAppStore().lang
  return cache.local.get('lang') || defaultSettings.defaultLang
})

import zhCn from './lang/zh-cn.json'
import en from './lang/en.json'
import zhTw from './lang/zh-tw.json'
import esES from './lang/es-ES.json'

import pageLoginCn from './pages/login/zh-cn.json'
import pageLoginEn from './pages/login/en.json'
import pageLoginTw from './pages/login/zh-tw.json'

import cks2409Cn from './pages/cks2409/zh-cn.json'
import cks2409En from './pages/cks2409/en.json'
import cks2409Es from './pages/cks2409/es-ES.json'
// 菜单页面
import pagemenuCn from './pages/menu/zh-cn'
import pagemenuEn from './pages/menu/en'
import pagemenuTw from './pages/menu/zh-tw'

const i18n = createI18n({
  // 全局注入 $t 函数
  globalInjection: true,
  fallbackLocale: 'zh-cn',
  locale: language.value, //默认选择的语言
  legacy: false, // 使用 Composition API 模式，则需要将其设置为false
  messages: {
    'zh-cn': {
      ...zhCn,
      ...pageLoginCn,
      ...pagemenuCn,
      ...cks2409Cn
    },
    'zh-tw': {
      ...zhTw,
      ...pageLoginTw,
      ...pagemenuTw
    },
    en: {
      ...en,
      ...pageLoginEn,
      ...pagemenuEn,
      ...cks2409En
    }
    //... 在这里添加其他语言支持
    ,'es-ES':{
      ...esES,
      ...cks2409Es
    }
  }
})

const  loadLocale =async () => {
  console.log('调用 loadLocale')
  
  let res = await listLangByLocale(language.value)
  const { code, data } = res
    if (code == 200) {
      
      i18n.global.mergeLocaleMessage(language.value, data)
    }
}

loadLocale()
export default i18n
